package all.com.jinyongqunxiax.model.mod;

import com.randy.fundation.JxAssert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import all.com.jinyongqunxiax.BuildConfig;
import androidx.annotation.NonNull;
import jy.game.components.shop.JyShop;
import jy.game.tools.BasePojoManager;

public class ShopManager {
    public Map<String, JyShop> map  = new HashMap<>();
    public List<JyShop>        list = new ArrayList<>();
    String                  xmlPath;
    BasePojoManager<JyShop> manager;

    public ShopManager(@NonNull String xmlPath) {
        this.xmlPath = xmlPath;
    }

    public String load() {
        map.clear();
        list.clear();

        manager = BasePojoManager.build(xmlPath, JyShop.class);
        list = manager.datas;
        if (!manager.success()) {
            return manager.message;
        }
        StringBuilder message = new StringBuilder(getClass().getSimpleName());

        for (JyShop item : list) {
            if (map.containsKey(item.name)) {
                message.append("发现重复:").append(item.name);
                continue;
            }
            map.put(item.name, item);
        }


        return manager.message;
    }

//
//    public String load() {
//        try {
//            map.clear();
//            list.clear();
//            File file = new File(xmlPath);
//
//            Serializer serializer = new Persister();
//            Root       root       = serializer.read(Root.class, file);
//            assert !BuildConfig.DEBUG || !root.models.isEmpty();
//            list = root.models;
//            for (Model model : list) {
//                map.put(model.name, model);
//            }
//            if (BuildConfig.DEBUG) {
//                assert map.entrySet().size() == list.size();
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            return e.getMessage();
//        }
//        return "";
//    }
//
//    static public class Root {
//        @ElementList(inline = true) List<Model> models;
//    }
//
//    @ wx.xml.simpleframework.xml.Root(name = "shop")
//    static public class Model {
//        public @Attribute                  String     name;
//        public @Attribute(name = "pic")    String     icon;
//        public @Attribute                  String     music;
//        public @ElementList(inline = true) List<Sale> sales;
//    }
//
//    @ wx.xml.simpleframework.xml.Root(name = "sale")
//    static public class Sale {
//        public @Attribute String name;
//        public @Attribute int    limit;
//        public @Attribute int    price;
//        public @Attribute int    yuanbao;
//    }
}
